{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e16b546e",
   "metadata": {},
   "source": [
    "## random函数讲解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a3dcf36b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ae7ee6d",
   "metadata": {},
   "source": [
    "### 1. numpy.random.seed函数和random.rand()函数\n",
    "numpy下的隨機數屬於偽隨機數，即给定种子下生成的随机数是相同的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "639bbee9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.34556072704304774"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#random.rand()生成给定维度的数组。不给定维度时生成一个python浮点数。\n",
    "np.random.rand()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "9f69aa3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.39676747, 0.53881673, 0.41919451],\n",
       "       [0.6852195 , 0.20445225, 0.87811744]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#可以生成多维数组\n",
    "np.random.rand(2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "ab999107",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.417022004702574"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#设置seed后随机数不变(所有平台都不变)\n",
    "#没有设置时，随机数产生的种子是根据系统的时间戳生成的\n",
    "np.random.seed(1)\n",
    "np.random.rand()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76583491",
   "metadata": {},
   "source": [
    "### 2. random.randn标准正态分布的随机函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3def77a",
   "metadata": {},
   "source": [
    "### 3. random.randint生成整型随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ee0d7c1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 5, 0],\n",
       "       [0, 1, 7]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "randint(low, high=None, size=None, dtype=int)\n",
    "low,high两参数给定随即区间。\n",
    "size=(a,b),此参数给定生成数组的维度情况。\n",
    "dtype参数确定数据类型，只能在int数据类型中选择，否则报错。\n",
    "'''\n",
    "#生成两行三列的随机数组，每个值在0-9的整数。\n",
    "np.random.randint(0,9,size=(2,3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02ed6f13",
   "metadata": {},
   "source": [
    "### 4.random.choice()函数\n",
    "\n",
    "choice(a, size=None, replace=True, p=None)\n",
    "a:数据源，可以是一个数组(需要一维)，也可以是一个数组表示生成0到这个数字的随机选择数。\n",
    "size:选择数据后生成数据的维度。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "85493e9c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6, 1, 0, 1, 8, 8, 3, 8, 7, 3])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# choice函数在序列中随即选择几个数字组成行列数组\n",
    "data = np.random.randint(0,9,size=(10,))\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "bbd55e50",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 1, 8])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#随机选取生成一维数组\n",
    "np.random.choice(data,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "a4cfb8b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 6, 8],\n",
       "       [3, 0, 8]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#随机选择生成二维数组\n",
    "np.random.choice(data,size=(2,3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1a37634",
   "metadata": {},
   "source": [
    "### 5.random.shuffle()把原来元素的位置打乱\n",
    "\n",
    "shuffle(x)\n",
    "x:需要打乱的数组\n",
    "\n",
    "This function only shuffles the array along the first axis of a multi-dimensional array. \n",
    "\n",
    "这个函数只能打乱包含在多维数组中的第一维度。\n",
    "\n",
    "此函数直接更新数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "84be261f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 7, 7, 7],\n",
       "       [3, 0, 8, 7],\n",
       "       [7, 1, 1, 3]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2 = np.random.randint(0,9,size=(3,4))\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "00dd4525",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 1, 1, 3],\n",
       "       [2, 7, 7, 7],\n",
       "       [3, 0, 8, 7]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#打乱二维数组，只是打乱了不同块的顺序\n",
    "np.random.shuffle(data2)\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "050300be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data3=np.arange(9)\n",
    "data3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "ee950a2a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 0, 1, 7, 6, 5, 3, 4, 2])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#一维数组是打乱内部数字\n",
    "np.random.shuffle(data3)\n",
    "data3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "cde1062c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function shuffle:\n",
      "\n",
      "shuffle(...) method of numpy.random.mtrand.RandomState instance\n",
      "    shuffle(x)\n",
      "    \n",
      "    Modify a sequence in-place by shuffling its contents.\n",
      "    \n",
      "    This function only shuffles the array along the first axis of a\n",
      "    multi-dimensional array. The order of sub-arrays is changed but\n",
      "    their contents remains the same.\n",
      "    \n",
      "    .. note::\n",
      "        New code should use the ``shuffle`` method of a ``default_rng()``\n",
      "        instance instead; please see the :ref:`random-quick-start`.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    x : ndarray or MutableSequence\n",
      "        The array, list or mutable sequence to be shuffled.\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    None\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    Generator.shuffle: which should be used for new code.\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> arr = np.arange(10)\n",
      "    >>> np.random.shuffle(arr)\n",
      "    >>> arr\n",
      "    [1 7 5 2 9 4 3 6 0 8] # random\n",
      "    \n",
      "    Multi-dimensional arrays are only shuffled along the first axis:\n",
      "    \n",
      "    >>> arr = np.arange(9).reshape((3, 3))\n",
      "    >>> np.random.shuffle(arr)\n",
      "    >>> arr\n",
      "    array([[3, 4, 5], # random\n",
      "           [6, 7, 8],\n",
      "           [0, 1, 2]])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(np.random.shuffle)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
